/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.Dosen;
import Entity.User;
import com.mysql.jdbc.PreparedStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import library.Koneksi;


/**
 *
 * @author mr2
 */
public class UserModel {
    
    Connection koneksi;
    User user;
    
    public UserModel(){
        koneksi = Koneksi.getKoneksi();
    }
    
    public List<User> listUser(){
        
        java.sql.PreparedStatement prepare = null;
        ResultSet result = null;
        List<User> listDosen = new ArrayList<User>();
        
        try{
            String sql = "SELECT * from user";
            
            prepare = koneksi.prepareStatement(sql);
            
            result = prepare.executeQuery();
            
            while(result.next()){
                
                User user = new User();
                user.setIdUser(result.getString("ID_USER"));
                user.setNamaUser(result.getString("NAMA_USER"));
                
                listDosen.add(user);
            }
            return listDosen;
            
        }catch(SQLException ex){
            System.out.print(ex.getMessage());
            return listDosen;            
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    
                }
            }
            if(result != null){
                try{
                    result.close();
                }catch(SQLException ex){
                    
                }
            }
        }
    }
    
    public boolean cekLogin(User user){
        boolean hasil = false;
        String sql = "SELECT * FROM user where user.EMAIL_USER='"+user.getEmailUser()+"' and user.PASSWORD ='"+user.getPassword()+"'";
        PreparedStatement st = null;
        ResultSet rs = null;
        try {

            Connection conn = Koneksi.getKoneksi();

            st = (PreparedStatement) conn.prepareStatement(sql);
            rs = st.executeQuery();
            
            rs.last();
            if(rs.getRow() > 0){
                hasil = true;
            }else{
                hasil = false;
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(UserModel.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                st.close();
            } catch (SQLException ex) {
                Logger.getLogger(UserModel.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                rs.close();
            } catch (SQLException ex) {
                Logger.getLogger(UserModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return hasil;
    }
    
    public User getLogin(User user){
        String sql = "SELECT * FROM user where user.EMAIL_USER='"+user.getEmailUser()+"' and user.PASSWORD='"+user.getPassword()+"'";
        PreparedStatement st = null;
        ResultSet rs = null;
        System.out.println("sqlnyoooo "+sql);
        try {

            Connection conn = Koneksi.getKoneksi();

            st = (PreparedStatement) conn.prepareStatement(sql);
            rs = st.executeQuery();
            
            while(rs.next()){
                user.setIdUser(rs.getString("ID_USER"));
                user.setNamaUser(rs.getString("NAMA_USER"));
                user.setEmailUser(rs.getString("EMAIL_USER"));
            }            
            
        } catch (SQLException ex) {
            Logger.getLogger(UserModel.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                st.close();
            } catch (SQLException ex) {
                Logger.getLogger(UserModel.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                rs.close();
            } catch (SQLException ex) {
                Logger.getLogger(UserModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return user;
    }
    
    public List getListRole(User user){
        List role = new ArrayList();
        String sql = "SELECT * FROM user, role_user, role where user.ID_USER = role_user.ID_USER and role_user.ID_ROLE = role.ID_ROLE and user.ID_USER='"+user.getIdUser()+"'";
        PreparedStatement st = null;
        ResultSet rs = null;
        System.out.println("sqlnyiiiiiii "+sql);
        try {

            Connection conn = Koneksi.getKoneksi();

            st = (PreparedStatement) conn.prepareStatement(sql);
            rs = st.executeQuery();
            
            while(rs.next()){
                role.add(rs.getString("ROLE_USER"));
                System.out.println("ooooo "+rs.getString("ROLE_USER"));
            }            
            
        } catch (SQLException ex) {
            Logger.getLogger(UserModel.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                st.close();
            } catch (SQLException ex) {
                Logger.getLogger(UserModel.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                rs.close();
            } catch (SQLException ex) {
                Logger.getLogger(UserModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return role;
    }
}
